package view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JComponent;
import model.Environment;
import model.Model;

/**
 * DrawingScreen.java represents the part
 *  of the GUI that has the turtle, path, and drawing.
 *
 * @author Angelica Schwartz
 */
public class DrawingScreen extends JComponent implements Observer {

    /**
     * default serialVersionUID.
     */
    private static final long serialVersionUID = 1L;
    /**
     * model associated with this screen / tab.
     */
    private Model myModel;
    /**
     * Environment object to observe.
     */
    private Environment myFollowed;

    /**
     * Creates the screen with the drawing and path.
     *
     * @param size is the original size
     * @param m is the model
     */
    public DrawingScreen(Dimension size, Model m) {
        // set size
        setPreferredSize(size);
        setSize(size);
        myModel = m;
        myFollowed = myModel.getEnvironment();
        myFollowed.addObserver(this);
    }

    /**
     * Paint the contents of the canvas.
     *
     * Never called by us directly, instead called by Java runtime
     * when area of screen covered by this container needs to be
     * displayed (i.e., creation, uncovering, change in status)
     *
     * @param pen used to paint shape on the screen
     */
    @Override
    public void paintComponent(Graphics pen) {
        super.paintComponent(pen);
        myModel.paint(pen);
    }

    /**
     * implemented Observer interface method. Called when
     *  notifyObservers() is called when there is a
     *  change in the model.
     *
     *  @param arg0 is the Observable object
     *  @param arg1 is empty
     */
    @Override
    public void update(Observable arg0, Object arg1) {
        repaint();
    }
}